<template>
  <!-- 备件表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    :width="!props.isReadonly ? '600px' : '750px'"
    draggable
    @close="closeDialog"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="120px"
      v-show="!props.isReadonly"
    >
      <!-- <el-row>
        <el-col :span="24">
          <el-form-item label="备件种类编号" prop="assetTypeCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.assetTypeCode"
              placeholder="请输入备件种类编号"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="备件种类名称" prop="kindName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.kindName"
              placeholder="请输入备件种类名称"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="排序" prop="sort">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.sort"
              placeholder="请输入排序"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="启用状态" prop="enabled">
            <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-descriptions
      v-show="props.isReadonly"
      class="margin-top"
      :column="2"
      size="large"
      border
    >
      <!-- <el-descriptions-item>
        <template #label>
          <div class="cell-item">备件种类编号</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.assetTypeCode }}
        </div>
      </el-descriptions-item> -->
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">备件种类名称</div>
        </template>

        <div style="width: 100%; word-break: break-all">
          {{ formData.kindName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">排序</div>
        </template>
        {{ formData.sort }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">是否启用</div>
        </template>
        <el-tag v-show="formData.enabled === 1" type="">启用</el-tag>
        <el-tag v-show="formData.enabled !== 1" type="info">停用</el-tag>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建时间</div>
        </template>
        {{ formData.createTime }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人</div>
        </template>
        {{ formData.createdUserName }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新时间</div>
        </template>
        {{ formData.lastUpdateTime }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新人</div>
        </template>
        {{ formData.lastUpdatedUserName }}
      </el-descriptions-item>
      <el-descriptions-item :span="2">
        <template #label>
          <div class="cell-item">备注</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.remarks || "暂无备注" }}
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EQUDEVICETYPEEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  parentId: {
    type: String,
    default: () => {
      return "";
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  assetTypeCode: [
    { required: true, message: "请输入备件种类编码", trigger: "blur" },
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入备件种类编码",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  kindName: [
    { required: true, message: "请输入备件种类名称", trigger: "blur" },
    {
      required: true,
      pattern: "[^ \x20]+",
      message: "请输入备件种类名称",
      trigger: "blur",
    },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  sort: [
    { required: true, message: "请输入排序", trigger: "blur" },
    // {
    //   required: true,
    //   pattern: "[^ \x20]+",
    //   message: "请输入排序",
    //   trigger: "blur",
    // },
    // { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
    {
      validator: validateDegree,
      trigger: "blur",
    },
  ],
  enabled: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});
function validateDegree(rule: any, value: any, callback: (a?: any) => {}) {
  if (isNaN(parseFloat(value)) && isFinite(value)) {
    callback(new Error("请输入整数"));
  } else if (!/^[+-]?\d+?(e[+-]?\d+)?$/.test(value)) {
    callback(new Error("请输入整数"));
  } else if (Number(value) < 0) {
    callback(new Error("请输入大于0的整数"));
  } else {
    callback();
  }
}
const formRef = ref(ElForm);

let formData: any = reactive({
  id: "",
  version: "",

  kindName: "",
  parentKindId: "",
  allId: "",
  allCode: "",
  allName: "",
  sort: "",
  enabled: 1,
  parentId: "",
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,

        kindName,
        parentKindId,
        allId,
        allCode,
        allName,
        sort,
        enabled,
        parentId,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,

        kindName,
        parentKindId,
        allId,
        allCode,
        allName,
        sort,
        enabled,
        parentId,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();

  formData.id = "";
  formData.version = "";

  formData.kindName = "";
  formData.parentKindId = "";
  formData.allId = "";
  formData.allCode = "";
  formData.allName = "";
  formData.sort = "";
  formData.enabled = 1;
  formData.parentId = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

const parentIdDialog = ref("");
watch(
  () => props.parentId,
  (newVal: string) => {
    parentIdDialog.value = newVal;
  }
);
const isReadonlyDialog = ref(false);
watch(
  () => props.isReadonly,
  (newVal: boolean) => {
    isReadonlyDialog.value = newVal;
  }
);
/** 备件种类保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      // 添加子机构
      const text = ref("");
      if (parentIdDialog.value) {
        formData.parentKindId = parentIdDialog.value;
        loading.value = true;
        text.value = "添加子备件种类";
      } else if (formData.id) {
        loading.value = true;
        text.value = "编辑";
      } else {
        loading.value = true;
        formData.parentKindId = "0";
        text.value = "新增";
      }
      loading.value = false;
      // isReadonlyDialog.value = false;
      await request({
        url: "/eam/parePartsType/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text.value + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
<style lange="scss" scoped>
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 120px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 180px;
}
</style>
